package com.we823.project.repository.service.product;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import com.we823.project.common.web.status.TaskStatus;
import com.we823.project.model.product.PlanDetailModel;
import com.we823.project.repository.dao.product.PlanDetailRepository;
import com.we823.project.repository.service.AbstractBaseService;

@Service("planDetailService")
public class PlanDetailService extends AbstractBaseService<PlanDetailModel> {

	private PlanDetailRepository planDetailRepository;
	
	@Autowired
	public PlanDetailService(PlanDetailRepository planDetailRepository) {
		super(planDetailRepository);
		this.planDetailRepository = planDetailRepository;
	}

	public List<PlanDetailModel> findByPlanId(Integer planId){
		return planDetailRepository.findByPlanId(planId);
	}

	public Page<PlanDetailModel> findByPlanId(Integer id, PageRequest pageRequest) {
		return planDetailRepository.findByPlanId(id,pageRequest);
	}

	public List<PlanDetailModel> findByStatusAndStationIdIn(TaskStatus status, List<Integer> stationIds) {
		return planDetailRepository.findByStatusAndStationIdIn(status,stationIds);
	}

	public Page<PlanDetailModel> findByCreateDate(int year, int month, int day,
			PageRequest pageRequest) {
		Page<PlanDetailModel> planDetailPage = null;
		if(pageRequest!=null) {
			if(year>0 && month==0) {
				planDetailPage = planDetailRepository.findByCreateDate(year,pageRequest);
			}
			if(month>0 && day==0) {
				planDetailPage = planDetailRepository.findByCreateDate(year,month,pageRequest);
			}
			if(day>0) {
				planDetailPage = planDetailRepository.findByCreateDate(year,month,day,pageRequest);
			}
		}else {
			if(year>0 && month==0) {
				planDetailPage = new PageImpl<PlanDetailModel>(planDetailRepository.findByCreateDate(year));
			}
			if(month>0 && day==0) {
				planDetailPage = new PageImpl<PlanDetailModel>(planDetailRepository.findByCreateDate(year,month));
			}
			if(day>0) {
				planDetailPage = new PageImpl<PlanDetailModel>(planDetailRepository.findByCreateDate(year,month,day));
			}
		}
		return planDetailPage;
	}
	
	public Page<PlanDetailModel> findByCreateDateAndStationId(int year,
			int month, int day, int stationId, Pageable pageable) {
		Page<PlanDetailModel> planDetailPage = null;
		if (pageable!=null) {
			if (year>0 && month==0) {
				planDetailPage = planDetailRepository.findByCreateDateAndStationId(year,stationId,pageable);
			}
			if (month>0 && day==0) {
				planDetailPage = planDetailRepository.findByCreateDateAndStationId(year,month,stationId,pageable);
			}
			if (day>0) {
				planDetailPage = planDetailRepository.findByCreateDateAndStationId(year,month,day,stationId,pageable);
			}
		}else {
			if (year>0 && month==0) {
				planDetailPage = new PageImpl<PlanDetailModel>(planDetailRepository.findByCreateDateAndStationId(year,stationId));
			}
			if (month>0 && day==0) {
				planDetailPage = new PageImpl<PlanDetailModel>(planDetailRepository.findByCreateDateAndStationId(year,month,stationId));
			}
			if (day>0) {
				planDetailPage = new PageImpl<PlanDetailModel>(planDetailRepository.findByCreateDateAndStationId(year,month,day,stationId));
			}
		}
		
		return planDetailPage;
	}

	public List<PlanDetailModel> findByPlanIdAndDeepAndProcessIn(int planId,
			int deep, List<String> preProcessList) {
		return planDetailRepository.findByPlanIdAndDeepAndProcessIn(planId,deep,preProcessList);
	}

	public List<PlanDetailModel> findByPlanIdAndDeepAndProcessInAndOrderidLessThan(
			int planId, int deep, List<String> preProcessList, int orderid) {
		return planDetailRepository.findByPlanIdAndDeepAndProcessInAndOrderidLessThan(planId, deep, preProcessList,orderid);
	}

	public Page<PlanDetailModel> findByStationId(int stationId,
			PageRequest pageRequest) {
		if (pageRequest==null) {
			return new PageImpl<PlanDetailModel>(planDetailRepository.findByStationId(stationId));
		}
		return planDetailRepository.findByStationId(stationId,pageRequest);
	}

	public Page<PlanDetailModel> findByIdIn(List<Integer> planDetailIdList,Pageable pageable) {
		if (pageable==null) {
			return new PageImpl<PlanDetailModel>(planDetailRepository.findByIdIn(planDetailIdList));
		}
		return planDetailRepository.findByIdIn(planDetailIdList,pageable);
	}

	public List<PlanDetailModel> findByStationId(int stationId) {
		return planDetailRepository.findByStationId(stationId);
	}

	public List<PlanDetailModel> findByIdIn(List<Integer> planDetailIdList) {
		return planDetailRepository.findByIdIn(planDetailIdList);
	}

	public Page<PlanDetailModel> findByIdInAndStationId(
			List<Integer> planDetailIdList, int stationId,
			Pageable pageable) {
		if (pageable==null) {
			return new PageImpl<PlanDetailModel>(planDetailRepository.findByIdInAndStationId(planDetailIdList,stationId));
		}
		return planDetailRepository.findByIdInAndStationId(planDetailIdList, stationId, pageable);
	}

	public List<PlanDetailModel> findByPlanIdAndNextProcess(Integer planId,
			String nextProcess) {
		return planDetailRepository.findByPlanIdAndNextProcess(planId,nextProcess);
	}

	public Page<PlanDetailModel> findByCreateDateBetween(Date fromDate,
			Date endDate, Pageable pageable) {
		if (pageable==null) {
			return new PageImpl<PlanDetailModel>(planDetailRepository.findByCreateDateBetween(fromDate,endDate));
		}
		return planDetailRepository.findByCreateDateBetween(fromDate,endDate, pageable);
	}

	public Page<PlanDetailModel> findByCreateDateBetweenAndStationId(
			Date fromDate, Date endDate, int stationId, Pageable pageable) {
		if(pageable==null) {
			return new PageImpl<PlanDetailModel>(planDetailRepository.findByCreateDateBetweenAndStationId(fromDate, endDate,stationId));
		}
		return planDetailRepository.findByCreateDateBetweenAndStationId(fromDate,endDate,stationId, pageable);
	}

	

}
